﻿using FR.SAFTAnalyser.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace FR.SAFTAnalyser.ValueConverters
{
    public class GroupTotalSummaryConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (null == value)
                return "null";

            ReadOnlyObservableCollection<object> items = value as ReadOnlyObservableCollection<object>;
            IEnumerable<DocumentosAgrupados> documentos = items.OfType<DocumentosAgrupados>();
            IEnumerable<LinhasAgrupadas> linhas = items.OfType<LinhasAgrupadas>();

            if (documentos != null && documentos.Count() > 0)
            {
                var total = documentos.Sum(i => i.Total);
                var incidencia = documentos.Sum(i => i.Incidencia);
                var imposto = documentos.Sum(i => i.Imposto);

                return string.Format("TOTAL {0:c} | TOTAL S/ IVA {1:c} | TOTAL IMPOSTO {2:c}", total, incidencia, imposto);
            }
            else if (linhas != null && linhas.Count() > 0)
            {
                var total = linhas.Sum(i => i.Incidencia);

                return string.Format("TOTAL S/ IVA {0:c}", total);
            }

            return "";
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
}
